<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Mosaddek">
    <meta name="keyword" content="FlatLab, Dashboard, Bootstrap, Admin, Template, Theme, Responsive, Fluid, Retina">
    <link rel="shortcut icon" href="newtemplatesfiles5/img/favicon.html">

    <link rel="stylesheet" href="asserts/css/swiper-bundle.min.css">


    <title>扫描二维码</title>

    <!-- Bootstrap core CSS -->
    <link href="newtemplatesfiles5/css/bootstrap.min.css" rel="stylesheet">
    <link href="newtemplatesfiles5/css/bootstrap-reset.css" rel="stylesheet">
    <!--external css-->
    <link href="newtemplatesfiles5/assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
    <link href="newtemplatesfiles5/assets/jquery-easy-pie-chart/jquery.easy-pie-chart.css" rel="stylesheet" type="text/css" media="screen"/>
    <link rel="stylesheet" href="newtemplatesfiles5/css/owl.carousel.css" type="text/css">
    <!-- Custom styles for this template -->
    <link href="newtemplatesfiles5/css/style.css" rel="stylesheet">
    <link href="newtemplatesfiles5/css/style-responsive.css" rel="stylesheet" />

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 tooltipss and media queries -->
    <!--[if lt IE 9]>
    <script src="newtemplatesfiles5/js/html5shiv.js"></script>
    <script src="newtemplatesfiles5/js/respond.min.js"></script>
    <![endif]-->

</head>

<body>

<section id="container" class="">
    <!--main content start-->
    <section id="main-content">
        <section class="wrapper">
            <div class="row">
                <div class="col-lg-4"></div>
                <div class="col-lg-4">
                    <div class="auto">
                        <video id="video" width="300" height="300" autoplay></video>
                        <canvas id="canvas" width="300" height="300" style="display: none;"></canvas>
                    </div>
                </div>
                <div class="col-lg-4"></div>
                <button id="capture">开始扫描</button>
            </div>
        </section>
    <!--main content end-->
    </section>
</section>
<!-- js placed at the end of the document so the pages load faster -->
<script src="newtemplatesfiles5/js/jquery.js"></script>
<script src="newtemplatesfiles5/js/jquery-1.8.3.min.js"></script>
<script src="newtemplatesfiles5/js/bootstrap.min.js"></script>
<script src="newtemplatesfiles5/js/jquery.scrollTo.min.js"></script>
<script src="newtemplatesfiles5/js/jquery.nicescroll.js" type="text/javascript"></script>
<script src="newtemplatesfiles5/js/jquery.sparkline.js" type="text/javascript"></script>
<script src="newtemplatesfiles5/assets/jquery-easy-pie-chart/jquery.easy-pie-chart.js"></script>
<script src="newtemplatesfiles5/js/owl.carousel.js" ></script>
<script src="newtemplatesfiles5/js/jquery.customSelect.min.js" ></script>

<!--common script for all pages-->
<script src="newtemplatesfiles5/js/common-scripts.js"></script>

<!--script for this page-->
<script src="newtemplatesfiles5/js/sparkline-chart.js"></script>
<script src="newtemplatesfiles5/js/easy-pie-chart.js"></script>
<!--swiper js-->
<script src="js/swiper-bundle.min.js"> </script>
<script>
    var file ,stream;
    //访问用户媒体设备的兼容方法
    function getUserMedia(constraints, success, error) {
        if (navigator.mediaDevices.getUserMedia) {
            //最新的标准API
            navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
        } else if (navigator.webkitGetUserMedia) {
            //webkit核心浏览器
            navigator.webkitGetUserMedia(constraints,success, error)
        } else if (navigator.mozGetUserMedia) {
            //firfox浏览器
            navigator.mozGetUserMedia(constraints, success, error);
        } else if (navigator.getUserMedia) {
            //旧版API
            navigator.getUserMedia(constraints, success, error);
        }
    }

    let video = document.getElementById('video');
    let canvas = document.getElementById('canvas');
    let context = canvas.getContext('2d');

    function success(stream) {
        //兼容webkit核心浏览器
        let CompatibleURL = window.URL || window.webkitURL;
        //将视频流设置为video元素的源
        console.log(stream);
        stream = stream;
        //video.src = CompatibleURL.createObjectURL(stream);
        video.srcObject = stream;
        video.play();
    }

    function error(error) {
        console.log(`访问用户媒体设备失败${error.name}, ${error.message}`);
    }

    if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
        //调用用户媒体设备, 访问摄像头
        getUserMedia({video : {width: 480, height: 320}}, success, error);
    } else {
        alert('不支持访问用户媒体');
    }
    // base64转文件

    document.getElementById('capture').addEventListener('click', function () {
        var res;
        for(var i = 0 ; i < 5 ; i++){
            getAImage();
            res =submicQR();
            //alert();
            if(res != "error"){
                break;
            }
            var now = new Date();
            var newnow=new Date();
            console.log(now);
            console.log(newnow)
            while(newnow < now + 3000){
                newnow  = new Date();
                console.log("new  "+newnow)
            }
        }
        if(res != "error"){
            location.href=res;
        }else{
            alert("扫描失败");
        }

    })


    function getAImage() {
        context.drawImage(video, 0, 0, 480, 320);
        // 获取图片base64链接
        var image = canvas.toDataURL('image/png');

        //将图片添加到页面中
        //document.body.appendChild(img);
        function dataURLtoFile(dataurl, filename) {
            var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            file = new File([u8arr], filename, {type: mime});
            return new File([u8arr], filename, {type: mime});
        }
        console.log(dataURLtoFile(image, 'aa.png'));
    }

    function submicQR(){
        var formData = new FormData();
        formData.append("file",file);
        var res = "error";
        $.ajax({
            type: "POST", // 数据提交类型
            url: "user-shibieQR.do", // 发送地址
            data: formData, //发送数据
            async: false, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false,
            success:function(data){
                //alert("ajax success");
                //stream.getTracks()[0].stop();//结束关闭流
                //location.href=data;
                res = data;
                console.log("ajax "+res);
            },
            error:function(e){
                //alert("识别失败");
                //stream.getTracks()[0].stop();//结束关闭流
                //alert("ajax error")
                res = "error";
            }
        });
        console.log("res "+res);
        return res;

    }

</script>
</body>
</html>
